/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.apache.log4j.Logger;
import queenb.web.store.LoginStore;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import org.ajax4jsf.component.html.HtmlAjaxCommandLink;
import queenb.net.entitybean.Login;
import queenb.web.session.LoginSession;

import queenb.web.util.SessionProvider;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.faces.context.ExternalContext;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import queenb.net.entitybean.*;
import queenb.web.crm.store.K400Store;
import queenb.web.crm.store.N850Store;
import queenb.web.store.N450Store;
import queenb.web.permission.store.Q150Store;
import queenb.web.store.V500Store;
import queenb.web.util.Util;

/**
 *
 * @author thanhct
 */
public class LoginCon extends QueenbController implements Serializable {

  

    private Logger logger = Logger.getLogger(LoginCon.class);
    private UIInput userNameInput;
    private UIInput passwordInput;
    private LoginStore loginStore;
    private LoginSession loginSession;
    private HtmlAjaxCommandLink loginCommandLink;
    private QueenbController queenbController;
    private int module;
    private String ipadress = "undefined";
    private String userName;
    private String password;
    private List<N450> list450;
    private List<N850> listN850;
    private List<V500> listV500;
    private List<Q400> listQ400;
    private String userNameLg;
    private int beginContructor;
    private String active="abc";
    
    private static Hashtable<String, HttpSession> sessionManager = new Hashtable<String, HttpSession>();

    public LoginCon() {
        super();
        System.out.println("construstor LoginCon---");
        
        
        if (loginStore == null) {
            loginStore = new LoginStore();
        }
       


    }

   
    public void checkLogin() {
       
        Login lg;
        Right right = new Right();
      
        try {
            //System.out.println("login module@userName:" + module + "@" + getUserName() + "ipppppppppppppppp" + ipadress);
            lg = new Login();
            lg.setModule(module);
            getLoginSession().setLogin(lg);
           
            lg = loginStore.checkLogin(7, getUserName(), getPassword());
            System.out.println("----------lg.getModule()->" + lg.getModule() + "--lg.getFk100()--" + lg.getFk100());
            setUserNameLg(lg.getUserName());
            if (getUserNameLg() == null || getUserNameLg().equals("")) {
                loginSession.setLogged(false);
                // getqueenbController().sendRedirect("login.jsp");
                logger.debug("login status--:" + lg.userName + " module:" + module);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login fail! Please, try again!", null));
            } else {
                getLoginSession().setLogin(lg);
                loginSession.setLogged(true);
                SessionProvider.setSessionMapValue("userName", userName);

                // force to expire the old session
                if (getSessionManager().containsKey(getUserNameLg())) {
                    HttpSession oldSession = getSessionManager().get(getUserNameLg());
                    oldSession.setMaxInactiveInterval(1);
                   

                }
                // add to the new session into session manager object
                FacesContext context = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
                getSessionManager().put(getUserNameLg(), session);

                //Right
                Q150Store store = new Q150Store();
                List<Q150> list = new ArrayList<Q150>();
                list = store.findQ175(7, 0, 0, lg.getId());
                
                right.setRight(list);
                getLoginSession().setRight(right);
                N450Store storeN450 = new N450Store();
                list450 = storeN450.findAllN450ForCombo(7, 99, getUserName());
                N850Store n850Store = new N850Store();

                listN850 = n850Store.findAllN850ForCombo(7, 99, 0, getUserName());
                SessionProvider.setSessionMapValue("listN850", listN850);
//                V500Store v500Store = new V500Store();
//                listV500 = v500Store.getListCombV050SGT(7, 99, null, "T", getUserName());
//
//                SessionProvider.setSessionMapValue("listV500", listV500);
                K400Store k400Store = new K400Store();
                listQ400 = k400Store.CombK400(7, 99, 0, getUserName());
                SessionProvider.setSessionMapValue("listQ400", listQ400);

            }
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Login Thanh Cong User:", "Login Thanh Cong User:"
                    + getLoginSession().getLogin().getUserName()));


            getqueenbController().sendRedirect("project.jsp?" + Util.convertMD5(queenbController.getRequest().getRequestURI()));

        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Login fail! Please, try again!",
                    "Login fail! Please, try again!"
                    + ex.getMessage()));
            logger.debug("checkLogin:Connect error. Try again"
                    + ex.getMessage());

        }
    }

    /**
     * @return the userNameInput
     */
    public UIInput getUserNameInput() {
        return userNameInput;
    }

    public String getInputStyleClass() {
        if (getUserNameInput().isValid()) {
            return "inputStyle";
        } else {
            return "inputErrorStyle";
        }
    }

    /**
     * @param userNameInput the userNameInput to set
     */
    public void setUserNameInput(UIInput userNameInput) {
        this.userNameInput = userNameInput;
    }

    /**
     * @return the passwordInput
     */
    public UIInput getPasswordInput() {
        return passwordInput;
    }

    /**
     * @param passwordInput the passwordInput to set
     */
    public void setPasswordInput(UIInput passwordInput) {
        this.passwordInput = passwordInput;
    }

    /**
     * @return the loginSession
     */
    public LoginSession getLoginSession() {
        if (loginSession == null) {

            setLoginSession(new LoginSession());
        }
        System.out.println("---session------");
        return loginSession;
    }

    /**
     * @param loginSession the loginSession to set
     */
    public void setLoginSession(LoginSession loginSession) {
        this.loginSession = loginSession;
    }

    /**
     * @return the loginCommandLink
     */
    public HtmlAjaxCommandLink getLoginCommandLink() {
        return loginCommandLink;
    }

    /**
     * @param loginCommandLink the loginCommandLink to set
     */
    public void setLoginCommandLink(HtmlAjaxCommandLink loginCommandLink) {
        this.loginCommandLink = loginCommandLink;
    }

    /**
     * @return the queenbController
     */
    public QueenbController getqueenbController() {
        if (queenbController == null) {
            queenbController = new QueenbController();
        }
        return queenbController;
    }

    /**
     * @return the module
     */
    public int getModule() {
        return module;
    }

    /**
     * @param module the module to set
     */
    public void setModule(int module) {
        this.module = module;
    }

    /**
     * @return the ipadress
     */
    public String getIpadress() {
        return ipadress;
    }

    /**
     * @param ipadress the ipadress to set
     */
    public void setIpadress(String ipadress) {
        this.ipadress = ipadress;
    }

    /**
     * @return the userName
     */
    public String getUserName() {

        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {

        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the list450
     */
    public List<N450> getList450() {
        return list450;
    }

    /**
     * @param list450 the list450 to set
     */
    public void setList450(List<N450> list450) {
        this.list450 = list450;
    }

    /**
     * @return the listN850
     */
    public List<N850> getListN850() {
        listN850 = (List<N850>) SessionProvider.getSessionMapValue("listN850");
        return listN850;
    }

    /**
     * @param listN850 the listN850 to set
     */
    public void setListN850(List<N850> listN850) {
        this.listN850 = listN850;
    }

    /**
     * @return the listV500
     */
//    public List<V500> getListV500() {
//        listV500 = (List<V500>) SessionProvider.getSessionMapValue("listV500");
//        return listV500;
//    }

    /**
     * @param aListV500 the listV500 to set
     */
    public void setListV500(List<V500> aListV500) {
        listV500 = aListV500;
    }

    /**
     * @return the listQ400
     */
    public List<Q400> getListQ400() {
        listQ400 = (List<Q400>) SessionProvider.getSessionMapValue("listQ400");
        return listQ400;
    }

    /**
     * @param listQ400 the listQ400 to set
     */
    public void setListQ400(List<Q400> listQ400) {
        this.listQ400 = listQ400;
    }

    /**
     * @return the userNameLg
     */
    public String getUserNameLg() {
        return userNameLg;
    }

    /**
     * @param userNameLg the userNameLg to set
     */
    public void setUserNameLg(String userNameLg) {
        this.userNameLg = userNameLg;
    }

    /**
     * @return the beginContructor
     */
    public int getBeginContructor() {
        return beginContructor;
    }

    /**
     * @param beginContructor the beginContructor to set
     */
    public void setBeginContructor(int beginContructor) {
        this.beginContructor = beginContructor;
    }
      /**
     * @return the sessionManager
     */
    public static Hashtable<String, HttpSession> getSessionManager() {
        return sessionManager;
    }

    /**
     * @param aSessionManager the sessionManager to set
     */
    public static void setSessionManager(Hashtable<String, HttpSession> aSessionManager) {
        sessionManager = aSessionManager;
    }

    /**
     * @return the active
     */
    public String getActive() {
       
        return active;
    }

    /**
     * @param active the active to set
     */
    public void setActive(String active) {
        this.active = active;
    }
}
